package nettyTest.netty4test1.decode;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SubReqServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscribeReq subscribeReq = (SubscribeReq) msg;
        if (subscribeReq.getSubReqId() == 5) {
            System.out.println(subscribeReq);
            ctx.writeAndFlush(resp(subscribeReq.getSubReqId()));
        }
    }

    private SubscribeResp resp(int subReqId) {
        SubscribeResp subscribeResp = new SubscribeResp();
        subscribeResp.setSubReqId(subReqId);
        subscribeResp.setRespCode(0);
        subscribeResp.setDesc("netty book");
        return subscribeResp;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
